## Milena Ang & Monika Nalepa
## April 2019
## Replication file for tables and figures of the paper
## "Can Transitional Justice Improve the Quality of Representation in New Democracies?"

# Libraries
library(dplyr)
library(tidyr)
library(foreign)
library(lme4)
library(stargazer, quietly = TRUE)
library(xtable)
library(stringr)
library(plotrix)

# Data:
# Set working directory
data <- read.csv("data.csv")

# Panel data:
tj_data <- read.csv("tj_data_panel.csv",
                    stringsAsFactors = FALSE) 

#############################################################################################################
## Figure 6 (Summary of regressive and progressive lustration events)

country_list <- unique(tj_data$country)

lab <- c(1960,1972, 1984, 1996, 2008)
s <- seq(1:5)

tj_data <- tj_data%>%
  mutate(xpos = tyear+yaftr,
         ypos = postrans+0.1,
         xneg = tyear+yaftr,
         yneg = -negtrans-0.1)

pdf("figure6.pdf", width=9, height = 5,
    family="Times", pointsize=10)
par(mfrow=c(9,7),
    mar=c(1,0.2,0.2,0.2),
    oma=c(3,0,0,0),
    mgp=c(1,0.2,1),
    bg = "white")

for (i in 1:length(country_list)) {
  plot(0,0, pch = "",
       xlim = c(1957, 2012),
       ylim = c(-4,9),
       xaxt = "n",
       yaxt="n")
  lines(tj_data$xpos[which(tj_data$country == country_list[i])],
        tj_data$ypos[which(tj_data$country == country_list[i])],
        col = "black")
  lines(tj_data$xneg[which(tj_data$country == country_list[i])],
        tj_data$yneg[which(tj_data$country == country_list[i])],
        col = "gray")
  
  segments(lab[s], -4, lab[s], 9,
           lty = "dotted", col = "grey")
  axis(1, at = lab, labels=FALSE,
       tick = FALSE)
  text(x = lab, y=par()$usr[3]-0.1*(par()$usr[4]-par()$usr[3]),
       labels = lab, cex = 0.5, srt=20, adj=1, xpd=TRUE)
  legend(x=1948,y=9,
         country_list[i],
         cex =0.85, fill = "white",
         box.col = "white")
}

par(fig = c(0, 1, 0, 1),
    oma = c(0, 0, 0, 0),
    mar = c(1, 0, 0, 1),
    new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend(0.77,-0.825, c("Positive events", "Negative events"), xjust = 1,
       xpd = TRUE, horiz = TRUE, inset = c(0,-0.02),
       lty = 1, col = c("black", "gray"), cex = 1.2)
dev.off()

#############################################################################################################
## Table 1

m_1 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity + 
              nyear +
              (1 | country),
            data = data)

m_4 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data = data)

m_5 <- lmer(cosal_3 ~ d_party_3 + miss_succ + 
              severity +
              nyear +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

## CREATING THE TABLE:
tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5,
                        header = FALSE, type = "html",
                        out = "table1.html",
                        title = "Table 1. Quality of representation and severity of lustration",
                        dep.var.captionß="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("\\emph{party distance}",
                                             "\\emph{severity}",
                                             "\\emph{years since transition}",
                                             "\\emph{press freedom}",
                                             "\\emph{opposition status}"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y"),
                                         c("Missing successor", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5"),
                        model.numbers = FALSE)

#############################################################################################################
## Figure 7 (Severity of lustration and average quality of democracy. Intercepts by country.)

coefs_m_1 <- as.matrix(ranef(m_1)[[1]])
coefs_m_1 <- as.data.frame(coefs_m_1)
coefs_m_1$country <- rownames(coefs_m_1)
names(coefs_m_1) <- c("coefs", "country")

data_plot <- data %>%
  group_by(country, ccodewb) %>%
  summarise(severity = mean(severity)) %>%
  inner_join(coefs_m_1)

attach(data_plot)
pdf("figure7.pdf",
    width = 8, height = 8,
    family="Times", pointsize=10)
par(mar=c(3,3,1,2),
    mgp=c(1.5,0.2,0))
plot(severity,coefs,
     pch = "",
     cex = 0.4,
     xlab= expression(paste("Severity of lustration (",
                            italic('severity'),")")),
     ylab = "Country coefficients (Model 1)",
     cex.lab = 1.5,
     cex.axis = 0.8)
text(severity,coefs,
     labels=ccodewb,cex = 0.6)
dev.off()
detach(data_plot)
